package com.shenghua.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Created by Administrator on 2018/8/28.
 */
public class RedisUtil {

    private JedisPool jedisPool;

    public JedisPool getJedisPool() {
        return jedisPool;
    }

    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    //get
    public String get(String key){
        Jedis jedis = jedisPool.getResource();
        String result = jedis.get(key);
        jedisPool.returnResource(jedis);
        return result;
    }

    //set
    public String set(String key,String value){
        Jedis jedis = jedisPool.getResource();
        String result = jedis.set(key,value);
        jedisPool.returnResource(jedis);
        return result;
    }

    /**
     * 设置redis缓存
     * @param key 关键字
     * @param seconds 秒
     * @param value 值
     * @return
     */
    public String set(String key,int seconds, String value){
        Jedis jedis = jedisPool.getResource();
        String result = jedis.setex(key,seconds,value);
        jedisPool.returnResource(jedis);
        return result;
    }


    //exists
    public boolean exists(String key){
        Jedis jedis = jedisPool.getResource();
        boolean result = jedis.exists(key);
        jedisPool.returnResource(jedis);
        return result;
    }

    //ttl
    public long ttl(String key){
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.ttl(key);
        jedisPool.returnResource(jedis);
        return result;
    }

    //del
    public long del(String key){
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.del(key);
        jedisPool.returnResource(jedis);
        return result;
    }

}
